/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: UserDAO.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.user.dao 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年7月23日 下午12:22:36 
 * @version: V1.0   
 */
package com.zizo.autohome.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.zizo.autohome.user.bean.User;
import com.zizo.core.DAO;
import com.zizo.core.T;

/**
 * @ClassName: UserDAO
 * @Description:
 * @author: Alexis
 * @date: 2016年7月23日 下午12:22:36
 */
public class UserDAO extends DAO {

	/**
	 * @Title:UserDAO
	 * @Description:
	 * @param currConn
	 */
	public UserDAO(Connection currConn) {
		super(currConn);
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getIDs
	 * 
	 * @Description:
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#getIDs()
	 */
	@Override
	public List<String> getIDs() {
		List<String> carIDs = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT user_uid FROM autohome_other.other_user WHERE user_uid>13798906";
		try {
			st = currConn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				carIDs.add(rs.getInt("user_uid") + " ");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return carIDs;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: insertData
	 * 
	 * @Description:
	 * 
	 * @param t
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#insertData(com.zizo.core.T)
	 */
	@Override
	public int insertData(T t) {
		PreparedStatement ps = null;
		String sql = "UPDATE autohome_other.other_user SET user_name=?,user_focus=?,user_fans=?,user_location=?,user_cksl=? WHERE user_uid=?";
		User user = (User) t;
		List<String> garages = user.getGarageIDs();
		List<String> certifications = user.getCertifications();
		for (String ck : garages) {
			insertCertifications(user.getUserID(), Integer.parseInt(ck), user.getMostFocus(), user.getRunning(), "ck");
		}
		for (String rz : certifications) {
			insertCertifications(user.getUserID(), Integer.parseInt(rz), user.getMostFocus(), user.getRunning(), "rz");
		}
		try {
			ps = currConn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setInt(2, user.getAttentionCount());
			ps.setInt(3, user.getFansCount());
			ps.setString(4, user.getLocation());
			ps.setInt(5, user.getGarageCount());
			ps.setInt(6, user.getUserID());
			if (user.getUserID() % 100000 == 0) {
				System.out.println(user.getUserID());
			}
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * @Title: insertCertifications
	 * @Description: 插入车库或者认证的相关信息
	 * @param userID
	 * @param ppid
	 * @param zgz
	 * @param zzk
	 * @param type
	 *            rz或者ck
	 * @return: void
	 * @author: Alexis
	 */
	public void insertCertifications(int userID, int ppid, int zgz, int zzk, String type) {
		PreparedStatement ps = null;
		String sql = "REPLACE INTO autohome_other.other_user_" + type + "(" + type + "_uid," + type + "_cid," + type + "_zgz,"
				+ type + "_driving) VALUES(?,?,?,?)";
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, userID);
			ps.setInt(2, ppid);
			ps.setInt(3, zgz);
			ps.setInt(4, zzk);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: updateTime
	 * 
	 * @Description:
	 * 
	 * @param type
	 * 
	 * @param id
	 * 
	 * @see com.zizo.core.DAOInterface#updateTime(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void updateTime(String type, String id) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getTables
	 * 
	 * @Description:
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#getTables()
	 */
	@Override
	public List<String> getTables() {
		return null;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: insertError
	 * 
	 * @Description:
	 * 
	 * @param t
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.DAOInterface#insertError(com.zizo.core.T)
	 */
	@Override
	public int insertError(T t) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * s * @Title: insertUserID
	 * 
	 * @Description: 只insert用户的ID，其余的信息空白。
	 * @param userID
	 * @return: void
	 * @author: Alexis
	 */
	public void insertUserID(String userID) {
		PreparedStatement ps = null;
		String sql = "REPLACE INTO autohome_other.other_user(user_uid) VALUES(?)";
		try {
			ps = currConn.prepareStatement(sql);
			ps.setInt(1, Integer.parseInt(userID));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
